import { Injectable } from '@nestjs/common';
import { CouponUserClient_Create_Dto, CouponUserClient_Update_Dto } from './assets/couponUserClient.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { CouponUserClient } from './assets/couponUserClient.entity';
import { Repository } from 'typeorm';
import { CouponUserClient_Find_Dto } from './assets/couponUserClient.dto';
import { util_Db_Add, util_Db_Delete, util_Db_PageList, util_Db_Update } from '@/common/util/db';
import { UserClient } from '../userClient/assets/userClient.entity';
import { Coupon } from '../coupon/assets/coupon.entity';
import { CouponUserClient__Util_Create_Class } from './assets/couponUseClient.class';
import { Constant_Enum_Enum_Coupon_TimeLimitType } from '@/common/constants/enum';

@Injectable()
export class CouponUserClient_Service {
  constructor(
    @InjectRepository(CouponUserClient)
    private coupon_Repository: Repository<Coupon>,
    @InjectRepository(CouponUserClient)
    private userClient_Repository: Repository<UserClient>,
    @InjectRepository(CouponUserClient)
    private couponUserClient_Repository: Repository<CouponUserClient>,
  ) {}
  async couponUserClient_Util_GetItemByCouponIdAndUserClientId(couponId: number, userClientId: number) {
    return await this.couponUserClient_Repository
      .createQueryBuilder('couponUserClient')
      .where(`couponUserClient.coupon = :coupon`, { coupon: couponId })
      .andWhere(`couponUserClient.userClient = :userClient`, { userClient: userClientId })
      .getOne();
  }

  couponUserClient_Util_Create(body: CouponUserClient__Util_Create_Class) {
    const couponUserClient = [];
    body.coupons.map((item) => {
      if (item.timeLimitType == Constant_Enum_Enum_Coupon_TimeLimitType.Coupon_TimeLimitType_ValidDays) {
        item.fixedTimeStart = new Date().getTime();
        item.fixedTimeEnd = item.validDays * 3600 * 24 * 1000 + new Date().getTime();
      }
      couponUserClient.push({
        fixedTimeEnd: item.fixedTimeEnd,
        fixedTimeStart: item.fixedTimeStart,
        userClient: body.userClient,
        coupon: item.id,
      });
    });
    return couponUserClient;
  }

  async couponUserClient_Create(body: CouponUserClient_Create_Dto) {
    const userClient = this.userClient_Repository.create({ id: body.userClientId });
    const coupon = this.coupon_Repository.create({ id: body.couponId });
    return await util_Db_Add({
      createDto: {
        userClient,
        coupon,
        fixedTimeStart: body.fixedTimeStart,
        fixedTimeEnd: body.fixedTimeEnd,
      },
      repository: this.couponUserClient_Repository,
    });
  }

  async couponUserClient_FindByPage(query: CouponUserClient_Find_Dto) {
    return await util_Db_PageList<CouponUserClient>({
      queryBuilder: this.couponUserClient_Repository
        .createQueryBuilder('couponUserClient')
        .leftJoin('couponUserClient.userClient', 'userClient')
        .leftJoin('couponUserClient.coupon', 'coupon')
        .select([
          'couponUserClient.id AS id',
          'couponUserClient.fixedTimeStart AS fixedTimeStart',
          'couponUserClient.fixedTimeEnd AS fixedTimeEnd',
          'couponUserClient.status AS status',
          'userClient.userName AS userName',
          'userClient.id AS userId',
          'coupon.id AS couponId',
        ])
        .where('coupon.id=:id', { id: query.couponId }),
      query,
      isRaw: true,
    });
  }

  async couponUserClient_FindOneById(id: number) {
    return await this.couponUserClient_Repository
      .createQueryBuilder('couponUserClient')
      .andWhere(`couponUserClient.id = :id`, { id: id })
      .getOne();
  }

  async couponUserClient_Update(body: CouponUserClient_Update_Dto) {
    return await util_Db_Update({
      findOne: () => this.couponUserClient_FindOneById(body.id),
      updateDto: body,
      repository: this.couponUserClient_Repository,
    });
  }

  async couponUserClient_Remove(id: number) {
    return await util_Db_Delete({
      findOne: () => this.couponUserClient_FindOneById(id),
      id,
      repository: this.couponUserClient_Repository,
    });
  }
}
